package org.noote.roby.core;

import org.noote.libs.bluetooth.DeviceList_Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Preferences_Activity extends Activity {
	private static final int GETTING_DEVICE = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        
        Button buttonBTDeviceSelect = (Button)findViewById(R.id.buttonBTDeviceSelect);
        buttonBTDeviceSelect.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	           Intent intent = new Intent(getApplicationContext(), DeviceList_Activity.class);
	           startActivityForResult(intent, GETTING_DEVICE);
			}
        });
        
        loadPreferences();
    }
    
    void loadPreferences()
    {
        SharedPreferences settings = getSharedPreferences(Main_Activity.PREFS_NAME, 0);
        
        String sCoreName = settings.getString(Main_Activity.PREFS_NAME_CORE_NAME, getString(R.string.preferences_core_name_default));
        String sServerPort = settings.getString(Main_Activity.PREFS_NAME_SERVER_PORT, getString(R.string.preferences_server_port_default));
        String sBTAddressDevice = settings.getString(Main_Activity.PREFS_BT_ADDRESS_DEVICE, getString(R.string.preferences_bluetooth_device_default));
    
        EditText editCoreName = (EditText)findViewById(R.id.editCoreName);
        editCoreName.setText(sCoreName);
        EditText editServerPort = (EditText)findViewById(R.id.editServerPort);
        editServerPort.setText(sServerPort);
        TextView textBTAddress = (TextView)findViewById(R.id.textBTAddress);
        textBTAddress.setText(sBTAddressDevice);
    }

    void savePreferences()
    {
        SharedPreferences settings = getSharedPreferences(Main_Activity.PREFS_NAME, 0);
        SharedPreferences.Editor settings_editor = settings.edit();
        
        EditText editCoreName = (EditText)findViewById(R.id.editCoreName);
        String sCoreName = editCoreName.getText().toString();
        EditText editServerPort = (EditText)findViewById(R.id.editServerPort);
        String sServerPort = editServerPort.getText().toString();
        TextView textBTAddress = (TextView)findViewById(R.id.textBTAddress);
        String sBTAddressDevice = textBTAddress.getText().toString();

        settings_editor.putString(Main_Activity.PREFS_NAME_CORE_NAME, sCoreName);
        settings_editor.putString(Main_Activity.PREFS_NAME_SERVER_PORT, sServerPort);
		settings_editor.putString(Main_Activity.PREFS_BT_ADDRESS_DEVICE, sBTAddressDevice);
		settings_editor.commit();
    }

	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data)
	{
		switch(requestCode)
		{
		case GETTING_DEVICE:
			if(resultCode == RESULT_OK)
			{
				String sBTAddressDevice = data.getStringExtra(DeviceList_Activity.EXTRA_DEVICE_ADDRESS);
				
		        TextView textBTAddress = (TextView)findViewById(R.id.textBTAddress);
		        textBTAddress.setText(sBTAddressDevice);
			}
			break;
		}
	}
    
    @Override
	public void onBackPressed()
	{
		savePreferences();
		setResult(RESULT_OK);
		super.onBackPressed();
	}
}
